package main

import "fmt"

func majorityElement(nums []int) int {
	count := make(map[int]int) //声明，初始化一个map
	majority := 0
	cnt := 0
	//利用hashmap的key值计算出现相同类型内容的合计,并判断出现最高次数的
	for i := 0; i < len(nums); i++ {
		count[nums[i]]++
		if count[nums[i]] > cnt {
			majority = nums[i]
			cnt = count[nums[i]]
		}
	}
	return majority
}

func main() {
	// nums := [...]int{1, 2, 3, 2, 2, 2, 5, 4, 2}
	nums := [...]int{1, 2, 3, 2, 2, 2, 5, 4, 3, 4, 1, 1, 2, 3, 4, 5, 3, 1, 2, 3, 23, 1, 2, 2, 1, 21, 2, 2, 3, 1, 2, 3, 2, 2, 1, 3, 1, 2, 3, 2, 1, 2, 3, 6, 6, 7, 8, 7, 6, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 82}
	fmt.Printf("nums is %d\n", nums)
	num := majorityElement(nums[:])
	fmt.Printf("num is %d\n", num)
}
